using Newtonsoft.Json;
using LiJ.Common.Core;
using LiJ.Http.Mos;
using LiJ.Framework.Official.Store.Mos;
using System.Threading.Tasks;

namespace LiJ.Framework.Official.Store
{
	/// <summary>
	/// 微信门店管理接口
	/// </summary>
	public class WxOffStoreApi : WxOffBaseApi
	{
		/// <summary>
		///  构造函数
		/// </summary>
		/// <param name="config">配置信息，如果这里不传，需要在程序入口静态 WxBaseApi.DefaultConfig 属性赋值</param>
		public WxOffStoreApi(AppConfig config = null)
			: base(config)
		{
		}

		/// <summary>
		/// 添加门店
		/// </summary>
		/// <param name="storeMo"></param>
		/// <returns></returns>
		public async Task<WxAddStoreResp> AddStoreAsync(WxStoreBasicSmallMo storeMo)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/poi/addpoi";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				business = new
				{
					base_info = storeMo
				}
			});
			return await base.RestCommonOffcialAsync<WxAddStoreResp>(LHttpRequest, null);
		}

		/// <summary>
		///   获取门店分店信息
		/// </summary>
		/// <param name="poiId"></param>
		/// <returns></returns>
		public async Task<WxGetStoreResp> GetStoreAsync(long poiId)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/poi/getpoi";
			LHttpRequest.CustomBody = string.Format("{{\"poi_id\":{0}}}", poiId);
			return await base.RestCommonOffcialAsync<WxGetStoreResp>(LHttpRequest, null);
		}

		/// <summary>
		///   获取门店分店列表信息
		/// </summary>
		/// <param name="begin">开始位置，0 即为从第一条开始查询</param>
		/// <param name="limit">返回数据条数，最大允许50，默认为20</param>
		/// <returns></returns>
		public async Task<WxGetStoreListResp> GetStoreListAsync(int begin = 0, int limit = 20)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/poi/getpoilist";
			LHttpRequest.CustomBody = string.Format("{{\"begin\":{0},\"limit\":{1}}}", begin, limit);
			return await base.RestCommonOffcialAsync<WxGetStoreListResp>(LHttpRequest, null);
		}

		/// <summary>
		///   修改门店基础信息
		/// </summary>
		/// <param name="serviceReq"></param>
		/// <returns></returns>
		public async Task<WxBaseResp> UpdateStoreBaicServiceAsync(WxUpdateStoreBasicServiceReq serviceReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/poi/updatepoi";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(serviceReq);
			return await base.RestCommonOffcialAsync<WxGetStoreResp>(LHttpRequest, null);
		}

		/// <summary>
		///   删除门店分店信息
		/// </summary>
		/// <param name="poiId"></param>
		/// <returns></returns>
		public async Task<WxBaseResp> DeleteStoreAsync(long poiId)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/poi/delpoi";
			LHttpRequest.CustomBody = string.Format("{{\"poi_id\":{0}}}", poiId);
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		///  获取门店类目列表
		/// </summary>
		/// <returns></returns>
		public async Task<WxStoreCategoryResp> GetStoreCategoryAsync()
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Get;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/poi/getwxcategory";
			return await base.RestCommonOffcialAsync<WxStoreCategoryResp>(LHttpRequest, null);
		}
	}
}
